package userInterface;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import staticClasses.Global;

public class SettingsPanel extends JPanel {
	
	private static final long serialVersionUID = 1L;

	private JPanel panelContainerSouth;	
	
	//Settings panel of the application
	public JPanel create() {
		
		//Create the panel
		JPanel settingsPanel = new JPanel();
				
	    //Title part (at the top of the panel)
	    JLabel title = new JLabel();
		String infoString = "<html> " +
								"<p align=\"center\"><font size=\"5\" color=\"#272F78\"><b>Game Settings</font></b></p><br /> " +
								"<p align=\"center\">Please complete all the parameters</p>" +
							"</html>";
		title.setText(infoString);
		
		//Buttons part (at the bottom of the panel)
        JButton quit = new JButton("Quit");
        quit.addActionListener(new QuitListener());
		JButton play = new JButton("Play game");
		
		//Teams' level part
		GridLayout teamLevelGrid = new GridLayout(2, 2); 
		JPanel teamLevelPanel = new JPanel(teamLevelGrid);
		JLabel team1LevelLabel = new JLabel("Team 1 :");
		JSlider team1LevelSlider = new JSlider(1, 3, 1);
		team1LevelSlider.setMinorTickSpacing(1);
		team1LevelSlider.setMajorTickSpacing(1);
		team1LevelSlider.setPaintTicks(true);
		team1LevelSlider.setPaintLabels(true);
		team1LevelSlider.addChangeListener(new Team1LevelSliderListener());
		JLabel team2LevelLabel = new JLabel("Team 2 :");
		JSlider team2LevelSlider = new JSlider(1, 3, 1);
		team2LevelSlider.setMinorTickSpacing(1);
		team2LevelSlider.setMajorTickSpacing(1);
		team2LevelSlider.setPaintTicks(true);
		team2LevelSlider.setPaintLabels(true);
		team2LevelSlider.addChangeListener(new Team2LevelSliderListener());
		teamLevelPanel.add(team1LevelLabel);
		teamLevelPanel.add(team1LevelSlider);
		teamLevelPanel.add(team2LevelLabel);
		teamLevelPanel.add(team2LevelSlider);
		teamLevelPanel.setBorder(BorderFactory.createTitledBorder("Teams' Level"));
		
		//Starting team Selection part
		GridLayout startingTeamGrid = new GridLayout(2, 2); 
		ButtonGroup startingTeamButtonGroup = new ButtonGroup();
		JRadioButton randomStartingButton = new JRadioButton("Random");
		JRadioButton startingTeamChoiceButton = new JRadioButton("Starting team :");
		startingTeamButtonGroup.add(randomStartingButton);
		startingTeamButtonGroup.add(startingTeamChoiceButton);			
		JLabel emptyLabel = new JLabel();
		String itemsTeam[] = new String[2];
		itemsTeam[0] = "Team 1";
		itemsTeam[1] = "Team 2";
		JComboBox startingTeamComboBox = new JComboBox(itemsTeam);
		JPanel startingTeamPanel = new JPanel(startingTeamGrid);
		startingTeamPanel.add(randomStartingButton);
		startingTeamPanel.add(emptyLabel);
		startingTeamPanel.add(startingTeamChoiceButton);
		startingTeamPanel.add(startingTeamComboBox);
		startingTeamPanel.setBorder(BorderFactory.createTitledBorder("Starting Team Selection"));
		
		//Game place selection part
		GridLayout gamePlaceGrid = new GridLayout(2, 1); 
		ButtonGroup gamePlaceButtonGroup = new ButtonGroup();
		JRadioButton team1GamePLaceButton = new JRadioButton("Team 1 soccer stadium");
		JRadioButton team2GamePLaceButton = new JRadioButton("Team 2 soccer stadium");
		gamePlaceButtonGroup.add(team1GamePLaceButton);
		gamePlaceButtonGroup.add(team2GamePLaceButton);
		JPanel gamePlacePanel = new JPanel(gamePlaceGrid);
		gamePlacePanel.add(team1GamePLaceButton);
		gamePlacePanel.add(team2GamePLaceButton);
		gamePlacePanel.setBorder(BorderFactory.createTitledBorder("Game Place"));
		
		//Game Duration part
		GridLayout gameDurationGrid = new GridLayout(1, 1);
		JPanel gameDurationPanel = new JPanel(gameDurationGrid);
		JSlider gameDurationSlider = new JSlider(1, 10, 1);
		gameDurationSlider.setMinimum(1);
		gameDurationSlider.setMajorTickSpacing(1);
		gameDurationSlider.setPaintTicks(true);
		gameDurationSlider.setPaintLabels(true);
		gameDurationPanel.add(gameDurationSlider);
		gameDurationPanel.setBorder(BorderFactory.createTitledBorder("Game Duration"));
		
		//place the different parts in the correct location of the main panel
		JPanel panelContainerNorth = new JPanel();    
		panelContainerNorth.setBorder(BorderFactory.createEmptyBorder(30, 0, 30, 0));
		panelContainerNorth.add(title);
		panelContainerSouth = new JPanel();
		panelContainerSouth.add(quit);
		panelContainerSouth.add(play);
		//place the 4 parameters parts in a parameters panel
		JPanel paramPanel = new JPanel(new GridBagLayout()); 
		GridBagConstraints paramGrid = new GridBagConstraints();	
		paramPanel.setPreferredSize(new Dimension(300, 500)); //400, 730
		paramGrid.fill = GridBagConstraints.HORIZONTAL;
		paramGrid.insets.left = 2; 
		paramGrid.insets.right = 2; 
		paramGrid.insets.bottom = 18;
		paramGrid.weightx = 1;
		paramGrid.gridx = 0; 
		paramGrid.gridy = 0; 
		paramPanel.add(teamLevelPanel, paramGrid);
		paramGrid.gridx = 0; 
		paramGrid.gridy = 1; 
		paramPanel.add(startingTeamPanel, paramGrid);
		paramGrid.gridx = 0; 
		paramGrid.gridy = 2; 
		paramPanel.add(gamePlacePanel, paramGrid);
		paramGrid.gridx = 0; 
		paramGrid.gridy = 3; 
		paramPanel.add(gameDurationPanel, paramGrid);
		settingsPanel.add(BorderLayout.NORTH, panelContainerNorth); 
		settingsPanel.add(BorderLayout.CENTER, paramPanel);		
	    settingsPanel.add(BorderLayout.SOUTH, panelContainerSouth); 

		return settingsPanel;
		
	}
	
	// ACTION PERFORMED //
      
	//action performed when you slide the team1Level slider
    class Team1LevelSliderListener implements ChangeListener {
        public void stateChanged(ChangeEvent e) {
            JSlider sourceSlider = (JSlider)e.getSource();
	        int value = (int)sourceSlider.getValue();
	        Global.team1Level = value;
        }
    }
    
  //action performed when you slide the team2Level slider
    class Team2LevelSliderListener implements ChangeListener {
        public void stateChanged(ChangeEvent e) {
            JSlider sourceSlider = (JSlider)e.getSource();
	        int value = (int)sourceSlider.getValue();
	        Global.team2Level = value;
        }
    }
    
  	//action performed when you click on or select quit
    class QuitListener implements ActionListener {
  		public void actionPerformed(ActionEvent event) {
  			System.exit(0);
  		}
  	}

}



